import * as Cesium from 'cesium';

class BaseCesiumClass {
  constructor(viewer, size) {
    this.defaultPos = [114.9358888888, 25.8458888888, 10]
    this.viewer = viewer;
    this.entity = null;
    this.size = size
  }

  createCube () {
    const position = Cesium.Cartesian3.fromDegrees(this.defaultPos[0], this.defaultPos[1], this.size / 2);
    this.entity = this.viewer.entities.add({
      position: position,
      box: {
        dimensions: new Cesium.Cartesian3(this.size, this.size, this.size),
        material: Cesium.Color.RED.withAlpha(0.5),
        outline: true,
        outlineColor: Cesium.Color.BLACK
      }
    });
  }
  createCubeByJson () {
  }
  cube (initialLongitude = 114.9358888888, initialLatitude = 25.8458888888, initialHeight = 10000, str) {
    // 创建初始立方体实体
    // var initialLongitude = 114.9358888888;
    // var initialLatitude = 25.8458888888;
    // var initialHeight = 10000;
    var dimensions = new Cesium.Cartesian3(300, 300, 300);
    // const numCubesTopBottom = 334;
    // const numCubesLeftRight = 1500;
    // const numCubesFrontBack = 1000;

    const minX = 0;
    const minY = 0;
    const minZ = 0;
    // X轴方向
    const numCubesLeftRight = 40;
    // Y轴方向
    const numCubesFrontBack = 40;
    // Z轴方向
    const numCubesTopBottom = 40;

    // 左右方向
    for (let i = minX; i < numCubesLeftRight; i++) {
      const longitudeLeft = initialLongitude - i * (dimensions.x / 100000);
      const positionLeft = Cesium.Cartesian3.fromDegrees(longitudeLeft, initialLatitude, initialHeight);
      this.viewer.entities.add({
        position: positionLeft,
        box: {
          dimensions: dimensions,
          material: Cesium.Color.RED.withAlpha(0.5),
          outline: true,
          outlineColor: Cesium.Color.BLACK
        }
      });

      const longitudeRight = initialLongitude + i * (dimensions.x / 100000);
      const positionRight = Cesium.Cartesian3.fromDegrees(longitudeRight, initialLatitude, initialHeight);
      this.viewer.entities.add({
        position: positionRight,
        box: {
          dimensions: dimensions,
          material: Cesium.Color.RED.withAlpha(0.5),
          outline: true,
          outlineColor: Cesium.Color.BLACK
        }
      });
    }

    // 前后方向
    for (let j = minY; j < numCubesFrontBack; j++) {
      const latitudeBack = initialLatitude - j * (dimensions.y / 100000) + (0.0003 * j);
      const positionBack = Cesium.Cartesian3.fromDegrees(initialLongitude, latitudeBack, initialHeight);
      this.viewer.entities.add({
        position: positionBack,
        box: {
          dimensions: dimensions,
          material: Cesium.Color.RED.withAlpha(0.5),
          outline: true,
          outlineColor: Cesium.Color.BLACK
        }
      });

      const latitudeFront = initialLatitude + j * (dimensions.y / 100000) - (0.0003 * j);
      const positionFront = Cesium.Cartesian3.fromDegrees(initialLongitude, latitudeFront, initialHeight);
      this.viewer.entities.add({
        position: positionFront,
        box: {
          dimensions: dimensions,
          material: Cesium.Color.RED.withAlpha(0.5),
          outline: true,
          outlineColor: Cesium.Color.BLACK
        }
      });
    }

    // 上下方向
    for (let k = minZ; k < numCubesTopBottom; k++) {
      var heightDown = initialHeight - k * dimensions.z;
      var positionDown = Cesium.Cartesian3.fromDegrees(initialLongitude, initialLatitude, heightDown);
      this.viewer.entities.add({
        position: positionDown,
        box: {
          dimensions: dimensions,
          material: Cesium.Color.RED.withAlpha(0.5),
          outline: true,
          outlineColor: Cesium.Color.BLACK
        }
      });

      var heightUp = initialHeight + k * dimensions.z;
      var positionUp = Cesium.Cartesian3.fromDegrees(initialLongitude, initialLatitude, heightUp);
      this.viewer.entities.add({
        position: positionUp,
        box: {
          dimensions: dimensions,
          material: Cesium.Color.RED.withAlpha(0.5),
          outline: true,
          outlineColor: Cesium.Color.BLACK
        }
      });
    }
  }
  removeCube () {
    if (this.entity) {
      this.viewer.entities.remove(this.entity);
      this.entity = null;
    }
  }
  flyToCube () {
    const position = Cesium.Cartesian3.fromDegrees(this.defaultPos[0], this.defaultPos[1], this.size / 2 + 11000);
    this.viewer.camera.flyTo({
      destination: position,
      orientation: {
        heading: Cesium.Math.toRadians(0.0),
        pitch: Cesium.Math.toRadians(-90.0),
        roll: 0.0
      },
      duration: 0
    });
  }
}

export default BaseCesiumClass;
